<?php
namespace app\we\controller;
use think\Controller;
use app\we\sdk\Thinkwechat;



class Index extends Controller{


    /**
     * 微信消息接口入口
     * 所有发送到微信的消息都会推送到该操作
     * 所以，微信公众平台后台填写的api地址则为该操作的访问地址
     * $this 498944516@qq.com
     */
    public function index($appid=0){
      
        //获取配置连接微信
        $this->appid=$appid;
        $options = model('We')->getOptions($appid)->toArray();
        $weObj = new Thinkwechat($options);
        $ret=$weObj->valid();
        if (!$ret) mylog(ErrCode::getErrText($weObj->errCode));
       
        //分解数据获得常用字段
        $this->openid = $weObj->getRev()->getRevFrom();
        $this->type = $weObj->getRev()->getRevType();
        $this->data = $weObj->getRev()->getRevData();
       
        //用户检测，如果有就存入data,没有则存入数据库
        $fans=model('WeFans')->info($this->openid )->toArray();
        if (!$fans){
           $fans=getUserInfo($this->appid,$this->openid );
           model('WeFans')->editData($fans); 
        }
        //补充常用相关数据到DATA
        $this->data['aid']=$options['aid'];
        $this->data['appid']=$options['appid'];
        $this->data['openid']=$fans['openid'];
        $this->data['nickname']=$fans['nickname'];
        $this->data['options']=$options;
        $this->data['fans']=$fans;
        

        //记录消息
        model('WeMessage')->saveMessage($this->data);

        $reply=$this->messageType();
        //处理兼容
        $reply['type']=isset($reply['type'])?$reply['type']:'text';
        $reply['message']=isset($reply['message'])?$reply['message']:'请记住代码和我爱着你';
      
       
        //回复粉丝
        $weObj->$reply['type']($reply['message'])->reply();

    }

    // 处理微信消息总入口
    public function messageType(){
       
        switch($this->type) {
            case Thinkwechat::MSGTYPE_TEXT:
                    $reply=['type'=>'text','message'=>$this->data['Content']];
                    break;
            case Thinkwechat::MSGTYPE_IMAGE:
                    $pic = $weObj->getMedia($data['MediaId']);
                    $data['pic']=$pic;
                    // $back=D('Weprinter/WeprinterOrder')->index($data); 
                    break;
            case Thinkwechat::MSGTYPE_VOICE:
                 
                    break;
            case Thinkwechat::MSGTYPE_MUSIC:
                   
                    break;
            
            case Thinkwechat::MSGTYPE_VIDEO:
                  
                    break;
            case Thinkwechat::MSGTYPE_LOCATION:
                  
                    break;
            case Thinkwechat::MSGTYPE_LINK:
                  
                    break;

            case Thinkwechat::MSGTYPE_EVENT:
              
                    break;
                    
        }

        return $reply;

    }

    // 处理微信事件消息
    public function messageEvent(){
       
        switch($this->data['Event']){
            case Thinkwechat::EVENT_SUBSCRIBE:
               
                
                 break;
            case Thinkwechat::EVENT_UNSUBSCRIBE:
               
                
                break;
            case Thinkwechat::EVENT_SCAN:
                
                 break;
            case Thinkwechat::EVENT_LOCATION:
               
                break;
            case Thinkwechat::EVENT_MENU_CLICK:

              
                break;
            case Thinkwechat::EVENT_MENU_SCAN_PUSH:
                 
                break;
            case Thinkwechat::EVENT_MENU_SCAN_WAITMSG:
               
                
                break;
            case Thinkwechat::EVENT_MENU_PIC_SYS:
                
                break;
            case Thinkwechat::EVENT_MENU_PIC_PHOTO:
               
                break;
            case Thinkwechat::EVENT_MENU_PIC_WEIXIN:
               
                break;
            case Thinkwechat::EVENT_MENU_LOCATION:
               
                break;
            case Thinkwechat::EVENT_SEND_MASS:
               
                break;
            case Thinkwechat::EVENT_SEND_TEMPLATE:
                
                break;
            case Thinkwechat::EVENT_KF_SEESION_CREATE:
                
                break;
            case Thinkwechat::EVENT_KF_SEESION_CLOSE:
                
                break;
            case Thinkwechat::EVENT_KF_SEESION_SWITCH:
                
                break;
            case Thinkwechat::EVENT_CARD_PASS:
                
                break;
            case Thinkwechat::EVENT_CARD_NOTPASS:
                
                break;
            case Thinkwechat::EVENT_CARD_USER_GET:
                
                break;
            case Thinkwechat::EVENT_CARD_USER_DEL:
                
                break;
            case Thinkwechat::WifiConnected :
                
                break;
            case Thinkwechat::ShakearoundUserShake :
                
                break;
            default:
                break;
                   
    
            }
        }

  

}